import {
    defineStore
} from "pinia";
import myAxios
    from "../http/myAxios";
import {
    responseDataHttp
} from "../type/response";
import {
    FindUserResponseData, FriendListType,
    FriendRequestList,
    FriendResponseData, UserResponseData
} from "../type/friend";
import {
    computed,
    ref
} from "vue";
import {showSuccessToast} from "vant";
import {chatType, friendChatTypeData} from "../type/chat";
import router
    from "../router";

// 好友

export const useFriendStore = defineStore('friend', () => {


    // 好友请求列表
    const friendRequestList = ref<FriendResponseData[]>()

    // 好友列表
    const friendList = ref<FriendListType[]>()

    // 与好友的聊天记录
    const friendChat = ref<friendChatTypeData>()


    // 获取好友列表
    async function getFriendList() {
        const result = await myAxios<responseDataHttp<{ friendList: FriendListType[] }>>("/auth/friend/friendList", {
            method: "GET"
        }).then(r => r.data)

        friendList.value = result.data.friendList
    }

    // 查找好友 通过账号
    async function getFindUser(account: string): Promise<FindUserResponseData> {
        const result = await myAxios<responseDataHttp<FindUserResponseData>>(`/auth/friend/findByAccount?account=${account}`, {
            method: "GET"
        }).then(r => r.data)

        return result.data
    }

    // 查找好友 通过id
    async function getFindUserById(id: number) {
        const result = await myAxios<responseDataHttp<{user:UserResponseData}>>(`/auth/friend/findById?id=${id}`, {
            method: "GET"
        }).then(r => r.data)

        // console.log(result)
        return result.data
    }

    //添加好友
    async function getAddFind(id: number, addInformation: string, note: string) {
        console.log(111)
        const result = await myAxios<responseDataHttp<{}>>(`/auth/friend/add?friend_id=${id}&add_information=${addInformation}&note=${note}`, {
            method: "GET"
        }).then(r => r.data)

        showSuccessToast(result.msg);
    }

    //删除好友
    async function delFriend(id:number){
        const data = new FormData()
        data.append("friend_id",String(id))

        const result  = await myAxios<responseDataHttp<{}>>("/auth/friend/delFriend",{
            method:"POST",
            data:data,
        }).then(r => r.data)

        await router.push({name:"home"})

        showSuccessToast(result.msg);

    }

    //查看好友请求列表
    async function getFriendRequestList() {
        const result = await myAxios<responseDataHttp<FriendRequestList>>(`/auth/friend/friendRequestList?type=1`, {
            method: "GET"
        }).then(r => r.data)

        friendRequestList.value = result.data.friendRequestList
        console.log(friendRequestList.value)
    }

    //同意好友申请
    async function getConsent(id: number, value: string) {
        const result = await myAxios<responseDataHttp<any>>("/auth/friend/agree", {
            method: "POST",
            headers: {
                "Content-Type": "application/json"
            },
            data: JSON.stringify({
                "id": id,
                "note": value,
                "status": 2
            })
        }).then(r => r.data)

        showSuccessToast(result.msg);
    }

    // 获取与好友的聊天记录
    async function getChat(id: number, page: number) {
        const result = await myAxios<responseDataHttp<friendChatTypeData>>(`/auth/friend/getChat?id=${id}&pageSize=10&page=${page}`).then(r => r.data)

        friendChat.value = result.data
        // console.log(result)
    }

    // 将聊天消息追加
    function pushFriendChat(chat: chatType){
        friendChat.value?.chat.chat.push(chat)
    }

    // 获取未读的消息数量
    const unReadCount = computed(()=>{
        let count = 0
        friendList.value?.forEach((e)=>{
            count += e.unread
        })

        return count
    })

    return {
        friendRequestList,
        friendList,
        friendChat,
        getFriendList,
        getFindUser,
        getFindUserById,
        getAddFind,
        getFriendRequestList,
        getConsent,
        getChat,
        pushFriendChat,
        unReadCount,
        delFriend
    }
})